#pragma once

#include <QFrame>
#include <QLabel>
#include <QMenu>
#include <QMouseEvent>
#include <QTimer>
#include <functional>
#include <vector>

#include "QFlowLayout.h"
#include "widgets.h"

using funcMenuCheck = std::function<void(int)>;

struct SFilterSubMenuItem {
  QString name;
  int id;
};

class SFilterLabel : public QLabel {
  Q_OBJECT
 public:
  SFilterLabel(int index, const QString&, int id = 0, std::string userdata = "",
               std::vector<SFilterSubMenuItem>* vctSub = nullptr,
               QWidget* parent = nullptr, funcMenuCheck func = nullptr);
  ~SFilterLabel();

  int index() const { return m_index; }
  int id() const { return m_id; }
  std::string userdata() { return m_userdata; }
  bool hasSubMenu() { return (int)m_vctSub.size() > 0; }
  void selAllMenuItems(bool check);
  void getChecked(std::vector<bool>& vctChecked);
  void getChecked(std::vector<int>& vctChecked);
  void setLabelEnable(bool enable);

 Q_SIGNALS:
  void clicked(int, bool);

 protected:
  void paintEvent(QPaintEvent*) override;
  void mouseMoveEvent(QMouseEvent* event) override;
  void mousePressEvent(QMouseEvent* event) override;
  void leaveEvent(QEvent* event) override;
  bool eventFilter(QObject* obj, QEvent* event) override;

  void onTimer();

 private:
  int m_index = 0;
  int m_id = 0;
  bool m_enable = true;
  std::string m_userdata;
  QString m_strStyle;
  std::vector<QAction*> m_vctSub;
  funcMenuCheck m_func = nullptr;
  QMenu* m_menu = nullptr;
  QTimer* m_timer = nullptr;
};

// namespace Ui{
// 	class SFilterButton;
// }

class S_WIDGETS_EXPORT SFilterButton : public QFrame {
  Q_OBJECT

 public:
  SFilterButton(QWidget* p, QString strBkColor = "TabBackNormal");

  void setName(const QString& sName);
  void setNameWidth(int width);
  void addTab(const QString& name, int id = 0, std::string userdata = "",
              std::vector<SFilterSubMenuItem>* vctSub = nullptr);
  void setTabFixedWidth(int width);
  void selectTab(int index, bool bCtrlPress = false);
  void selectAllTabs();
  void getSelTab(std::vector<int>& vctSel, bool includeSub = false);
  void getSelTabId(std::vector<int>& vctSelId);
  void getSelTabName(std::vector<QString>& vctSelName);
  void getSelTabUserData(std::vector<std::string>& vctSelUserData);
  void clearSelect();
  void changeSelect(int index, bool selected);
  void enableAllTabs();
  void disableAllTabs();
  void setMultiSel(bool multiSel);
  void setVirtualHead();
  void setVirtualHead2();
  void onMenuChecked(int index);
  bool isSelected(int index, bool includeSub = false);
  int getTabCount();

 Q_SIGNALS:
  void tabSelected(int index);

 protected:
  void paintEvent(QPaintEvent* event) override;

 protected Q_SLOTS:
  void onTabClicked(int, bool);

 protected:
  // QHBoxLayout* m_layout = nullptr;
  QFlowLayout* m_layout = nullptr;
  std::vector<SFilterLabel*> m_labels;
  QString m_styleSheet;
  QString m_name;
  QString m_strBkColor;
  int m_nameWidth = 0;
  int m_fixedWidth = 0;
  bool m_bMultiSelect = false;
  bool m_bVirtualHead = false;
  bool m_bVirtualHead2 = false;
};
